package router

import (
	"gin_vote/config"
	"gin_vote/controller"
	"gin_vote/pkg/logger"

	"github.com/gin-contrib/sessions"
	sessions_redis "github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	// 生成实例
	r := gin.Default()
	// 日志记录
	r.Use(gin.LoggerWithConfig(logger.LoggerToFile()))
	r.Use(logger.Recover)

	// 配置redis
	store, _ := sessions_redis.NewStore(10, "tcp", config.RedisAddress, "123456", []byte("secret"))
	r.Use(sessions.Sessions("mySession", store))
	// 接口分组，等同于java中的controller的样子
	user := r.Group("/user")
	{
		user.POST("/register", controller.UserController{}.Register)
		user.POST("/login", controller.UserController{}.Login)
	}

	player := r.Group("/player")
	{
		player.POST("/list", controller.PlayerController{}.GetPlayers)
		player.POST("/ranking", controller.PlayerController{}.GetRanking)
	}
	return r
}
